package com.xzsoft.gdwz.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xzsoft.gdwz.common.CommonUtil;
import com.xzsoft.gdwz.dao.OrderDao;
import com.xzsoft.gdwz.model.OrderHeader;
import com.xzsoft.gdwz.model.OrderLine;
import com.xzsoft.gdwz.services.JdOrderService;

@Transactional
@Service("jdOrderService")
public class JdOrderServiceImpl implements JdOrderService {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private CommonUtil commonUtil;
	
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = RuntimeException.class)
	public void saveOrder(JSONArray jsonArray) throws Exception {
		for(int j = 0 ; j<jsonArray.size(); j++){
			JSONObject map = (JSONObject)jsonArray.get(j);
			//订单主信息
			OrderHeader oh = new OrderHeader();
			oh.setEcommerceName("JD");
			oh.setEcommerceOrderId((String)map.get("jdOrderId"));
			oh.setInqHid(UUID.randomUUID().toString());
			oh.setInqHCode((String)map.get("jdOrderId"));
			oh.setInqAmount(map.get("orderPrice")+"");
			oh.setCreateDate((String)map.get("createorderTime"));
			oh.setCreateBy((String)map.get("username"));
			oh.setCreateByPhone((String)map.get(""));
			oh.setAddress((String)map.get(""));
			oh.setVendorName("京东");
			oh.setVendorPhone("");
			try {
				oh = commonUtil.initUserOrgMsg(oh);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//订单明细信息
			JSONArray array = (JSONArray)map.get("sku");
			//设置订单名称，第一行物资信息+项目单位简称
			if(array.size()>0){
				oh.setInqHName((String)((JSONObject)array.get(0)).get("name")+"("+oh.getInqHName()+")");
			}
			orderDao.insertOrderHeader(oh);
			for(int i = 0;i<array.size();i++){
				OrderLine ol = new OrderLine();
				JSONObject jo = (JSONObject)array.get(0);
				ol.setEcommerceName("JD");
				ol.setEcommerceSkuId((String)jo.get("skuId"));
				ol.setInqHid(oh.getInqHid());
				ol.setPrName((String)jo.get("name"));
				ol.setCcidId(UUID.randomUUID().toString());
				ol.setMtName((String)jo.get("name"));
				ol.setMtCode((String)jo.get("name"));
				ol.setReqSpec((String)jo.get("name"));
				ol.setReqModel((String)jo.get("name"));
				ol.setReqMaterial((String)jo.get("name"));
				ol.setReqUnit((String)jo.get("unit"));
				ol.setReqQuantity(jo.get("skunum")+"");
				ol.setInqPrice(jo.get("nakedPrice")+"");
				ol.setInqAmount((Integer)jo.get("nakedPrice")*(Integer)jo.get("skunum")+"");
				ol.setReqBrand((String)jo.get("name"));
				ol.setCreationDate((String)map.get("createorderTime"));
				ol.setCreatedBy((String)map.get("username"));
				try {
					ol = commonUtil.initUserOrgMsg(ol);
				} catch (Exception e) {
					e.printStackTrace();
				}
				orderDao.insertOrderLine(ol);
			}
		}
	}

	/**
	 * 获取订单信息
	 */
	@Override
	public List<OrderHeader> getOrder() {
		OrderHeader oh = new OrderHeader();
		List<OrderHeader> orderlist =  new ArrayList<OrderHeader>();
		orderlist = orderDao.SelectOrder();
		return orderlist;
	}
    
	/**
	 * 更改订单状态、is_ipdate字段
	 */
	@Override
	public void UpdateOrderStatus(String inqid) {
		orderDao.UpdateOrder(inqid); 
	}

	@Override
	public void saveOrder(JSONObject json) throws Exception {
		String createBy = json.getString("createBy");
		String createDate = json.getString("createDate");
		//如果是父订单
		int type = json.getInt("type");
		if(type == 1){
			JSONObject pOrder = json.getJSONObject("pOrder");
			OrderHeader oh = new OrderHeader();
			oh.setEcommerceName("JD");
			oh.setEcommerceOrderId(pOrder.get("jdOrderId")+"");
			oh.setInqHid(UUID.randomUUID().toString());
			oh.setInqHCode(pOrder.get("jdOrderId")+"");
			oh.setInqAmount(pOrder.get("orderPrice")+"");
			oh.setCreateDate(createDate);
			oh.setCreateBy(createBy);
			oh.setCreateByPhone((String)pOrder.get(""));
			oh.setAddress((String)pOrder.get(""));
			oh.setVendorName("京东");
			oh.setVendorPhone("");			
			oh = commonUtil.initUserOrgMsg(oh);
			JSONArray array = pOrder.getJSONArray("sku");
			//设置订单名称，第一行物资信息+项目单位简称
			if(array.size()>0){
				oh.setInqHName((array.getJSONObject(0).get("name")+"("+oh.getComName()+")-京东采购"));
			}
			orderDao.insertOrderHeader(oh);
			for(int i = 0;i<array.size();i++){
				OrderLine ol = new OrderLine();
				JSONObject jo = (JSONObject)array.get(i);
				ol.setEcommerceName("JD");
				ol.setEcommerceSkuId(jo.get("skuId")+"");
				ol.setInqHid(oh.getInqHid());
				ol.setPrName((String)jo.get("name"));
				ol.setCcidId(UUID.randomUUID().toString());
				ol.setMtName((String)jo.get("name"));
				ol.setMtCode((String)jo.get("name"));
				ol.setReqSpec((String)jo.get("name"));
				ol.setReqModel((String)jo.get("name"));
				ol.setReqMaterial((String)jo.get("name"));
				ol.setReqUnit((String)jo.get("unit"));
				ol.setReqQuantity(jo.get("num")+"");
				ol.setInqPrice(jo.getDouble("price")+"");
				ol.setInqAmount(jo.getDouble("price")*jo.getDouble("num")+"");
				ol.setReqBrand((String)jo.get("name"));
				ol.setCreationDate(createDate);
				ol.setCreatedBy(createBy);
				ol = commonUtil.initUserOrgMsg(ol);
				orderDao.insertOrderLine(ol);
			}
		}else if(type == 2){
			JSONObject pOrder = json;
			OrderHeader oh = new OrderHeader();
			oh.setEcommerceName("JD");
			oh.setEcommerceOrderId(pOrder.get("jdOrderId")+"");
			oh.setInqHid(UUID.randomUUID().toString());
			oh.setInqHCode(pOrder.get("jdOrderId")+"");
			oh.setInqAmount(pOrder.get("orderPrice")+"");
			oh.setCreateDate(createDate);
			oh.setCreateBy(createBy);
			oh.setCreateByPhone((String)pOrder.get(""));
			oh.setAddress((String)pOrder.get(""));
			oh.setVendorName("京东");
			oh.setVendorPhone("");			
			oh = commonUtil.initUserOrgMsg(oh);
			JSONArray array = pOrder.getJSONArray("sku");
			//设置订单名称，第一行物资信息+项目单位简称
			if(array.size()>0){
				oh.setInqHName((array.getJSONObject(0).get("name")+"("+oh.getComName()+")-京东采购"));
			}
			orderDao.insertOrderHeader(oh);
			for(int i = 0;i<array.size();i++){
				OrderLine ol = new OrderLine();
				JSONObject jo = (JSONObject)array.get(i);
				ol.setEcommerceName("JD");
				ol.setEcommerceSkuId(jo.get("skuId")+"");
				ol.setInqHid(oh.getInqHid());
				ol.setPrName((String)jo.get("name"));
				ol.setCcidId(UUID.randomUUID().toString());
				ol.setMtName((String)jo.get("name"));
				ol.setMtCode((String)jo.get("name"));
				ol.setReqSpec((String)jo.get("name"));
				ol.setReqModel((String)jo.get("name"));
				ol.setReqMaterial((String)jo.get("name"));
				ol.setReqUnit((String)jo.get("unit"));
				ol.setReqQuantity(jo.get("num")+"");
				ol.setInqPrice(jo.getDouble("price")+"");
				ol.setInqAmount(jo.getDouble("price")*jo.getDouble("num")+"");
				ol.setReqBrand((String)jo.get("name"));
				ol.setCreationDate(createDate);
				ol.setCreatedBy(createBy);
				ol = commonUtil.initUserOrgMsg(ol);
				orderDao.insertOrderLine(ol);
			}
		}
	}

}
